#include "stddef.h"
#include "Common.h"
#include "LCD.h"
#include "Gdi.h"


/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       GUIPROP_FindChar
*/
static const GUI_FONT_PROP  * GUIPROP_FindChar(const GUI_FONT_PROP * pProp, WORD c) {
    for (; pProp; pProp = pProp->pNext) {
        if ((c>=pProp->First) && (c<=pProp->Last))
            break;
    }
    return pProp;
}

/*********************************************************************
*
*       GUIPROP_FindChar
*/
static const int  GUI_FindChar(const WORD * aCharGroup, WORD key, DWORD uiSize) 
{
    int iLeft = 0, iRight = uiSize -1, iMid = 0;
    iMid = (iLeft + iRight)/2;

    //Uart0_Printf("GUI_FindChar %x %x\r\n", key, uiSize);

    while(iLeft < iRight && aCharGroup[iMid] != key)
    {
        if(aCharGroup[iMid] < key)
        {
            iLeft = iMid + 1;
        }
        else if(aCharGroup[iMid] > key)
        {
            iRight = iMid -1;
        }

        iMid = (iLeft + iRight)/2;
    }

    if(aCharGroup[iMid]  == key)
    {
        //Uart0_Printf("GUI_FindChar %x return %x \r\n", key, iMid);
        return iMid;
    }

    //Uart0_Printf("GUI_FindChar %x return %x \r\n", key, -1);
    return -1;
}


/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       GUIPROP_DispChar
*
* Purpose:
*   This is the routine that displays a character. It is used by all
*   other routines which display characters as a subroutine.
*/
void GUIPROP_DispChar(WORD c) {
    int BytesPerLine;
    //const GUI_FONT_PROP * pProp = GUIPROP_FindChar(GUI_Context.pAFont->p.pProp, c);
    const int iIndex = GUI_FindChar(GUI_Context.pAFont->p.pMap->pCharGBK, c , GUI_Context.pAFont->p.pMap->usCharsSize);
    //Uart0_Printf("GUIPROP_DispChar 0x%x\r\n", c);
    if (iIndex >= 0) {
        const GUI_CHARINFO * pCharInfo = GUI_Context.pAFont->p.pMap->paCharInfo + iIndex;
        BytesPerLine = pCharInfo->BytesPerLine;

        GUI_Context.pDeviceAPI->pfDrawBitmap( GUI_Context.DispPosX, GUI_Context.DispPosY,
                                              pCharInfo->XSize, GUI_Context.pAFont->YSize,
                                              BytesPerLine,
                                              pCharInfo->pData,
                                              1,
                                              NULL
                                              );
        /* Fill empty pixel lines */
        if (GUI_Context.pAFont->YDist > GUI_Context.pAFont->YSize) {
            int YMag = GUI_Context.pAFont->YMag;
            int YDist = GUI_Context.pAFont->YDist * YMag;
            int YSize = GUI_Context.pAFont->YSize * YMag;
        }

        GUI_Context.DispPosX += pCharInfo->XDist * GUI_Context.pAFont->XMag;
    }
    else
    {
        //Uart0_Printf("GUIPROP_DispChar can not find char data 0x%x\r\n", c);
    }
}

/*********************************************************************
*
*       GUIPROP_GetCharDistX
*/
int GUIPROP_GetCharDistX(WORD c) {
    //const GUI_FONT_PROP  * pProp = GUIPROP_FindChar(GUI_Context.pAFont->p.pProp, c);
    const int iIndex = GUI_FindChar(GUI_Context.pAFont->p.pMap->pCharGBK, c , GUI_Context.pAFont->p.pMap->usCharsSize);
    return (iIndex >= 0) ? (GUI_Context.pAFont->p.pMap->paCharInfo + iIndex)->XSize * GUI_Context.pAFont->XMag : 0;
}

/*********************************************************************
*
*       GUIPROP_GetFontInfo
*/
void GUIPROP_GetFontInfo(const GUI_FONT * pFont, GUI_FONTINFO* pfi) {
    pfi->Flags = GUI_FONTINFO_FLAG_PROP;
}

/*********************************************************************
*
*       GUIPROP_IsInFont
*/
char GUIPROP_IsInFont(const GUI_FONT  * pFont, WORD c) {
    //const GUI_FONT_PROP  * pProp = GUIPROP_FindChar(pFont->p.pProp, c);
    const int iIndex = GUI_FindChar(GUI_Context.pAFont->p.pMap->pCharGBK, c , GUI_Context.pAFont->p.pMap->usCharsSize);
    return (iIndex < 0) ? 0 : 1;
}

/*************************** End of file ****************************/
